<?php
namespace Admin\Controller;
use Think\Controller;
class VideoController extends Controller {
	public function videolist(){
	 $videomodel=M('video');
        //var_dump($usermodel);
      $video=$videomodel->table('video u,club c')->where("u.club_id=c.club_id")->field('u.*,c.club_name')->select();
        //调用视图层显示数据
        $this->video=$video;

        $this->display();

	}

	public function videoeslist(){
	 $videomodel=M('video');
        //var_dump($usermodel);
      $video=$videomodel->table('video u,club c')->where("u.club_id=c.club_id")->field('u.*,c.club_name')->select();
        //调用视图层显示数据
        $this->video=$video;

        $this->display();

	}

	public function addvideo(){
			$this->display();
	}
	public function do_addvideo(){
	     $data=$_POST;
		$file=$_FILES;
		$videoModel=M('video');
		$createresult=$videoModel->create();
		if($createresult){
			$videoModel->video_name=trim(I('post.video_name'));
			$videoModel->video_id=trim(I('post.video_id'));
			// $videoModel->video_video_type=trim(I('post.video_video_type'));
			$videoModel->video_videos=trim(I('post.video_videos'));
			$videoModel->video_time=date("Y-m-d H:i:s");
			if($file['video_videos']['error']==0){
				$info=$this->MyUpload();
				$videoModel->video_videos=$info['video_videos']['savename'];

			}else{
			$videoModel->video_videos="mao.mp4";
         
     }
    
      $addresult=$videoModel->add();

      if (false !== $addresult)
                 {
                    $this->success('添加成功！', '../Video/videolist');
                }else{ 
                   $this->error('数据错误'); 
               }


     } else{
            $this->error($videoModel->getError()); 	
	}


	}
	public function deleteVideo(){
		$video_id=I('video_id');
		$videoModel=M('video');
		$video=$videoModel->where("video_id=$video_id");
		$result=$videoModel->where("video_id=$video_id")->delete();
		// $picPath='__Public__/Admin/photo/'.$video[0]['video_videos'];
		// unlink($picPath);
		if($result){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}

	}
	public function editvideo(){
		$video_id=I('video_id');
		$videoModel=M('video');
		$video=$videoModel->where("video_id=$video_id")->select();

		//dump($video);
		$this->video=$video;
		$this->display();

	}
	public function updatevideo(){
		$data=$_POST;
		$file=$_FILES;
		//dump($file);
		$video_id=$data['video_id'];
		//dump($video_id);
		$videoModel=M('video');
		if($file['video_videos']['size']==0){
			$result=$videoModel->where("video_id=$video_id")->save($data);
		}else{
			//修改头像
			$upload= new \Think\Upload();//实例化上传类
			$upload->maxSize = 31457280000000;
			$upload->allowExts  = array('mp4', 'rmvb', 'mkv', 'f4v','avi','3gp','swf','mpeg','rm','jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Admin/photo/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
				 //保存表单数据 包括附件数据
               	$data['video_videos']= $info['video_videos']['savename']; 
                $result=$videoModel->where("video_id=$video_id")->save($data);
                	if ($data['oldpic']!='mao.mp4') {
                	$oldpicPath='Public/Admin/'.$data['oldpic'];
                	unlink($oldpicPath);
               		 	}
            		}
        	}
        	//dump($data);
		if($result){
       		 $this->success('修改成功！','../Video/videolist');
     		 }
      		else{
        		$this->error('修改失败！');
      			}
}



	    public function MyUpload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 31457280000 ;// 设置附件上传大小
              $upload->allowExts  = array('mp4', 'rmvb', 'mkv', 'f4v','avi','3gp','swf','mpeg','rm');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Admin/photo/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 
                        return $info;
                    }
                } 


}

?>